﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace l1cs.server.utils
{
    class SqlUtils
    {
        public SqlUtils() { }

        public static int toInt( Object obj )
        {
            int ret;
            if ( obj is DBNull )
            {
                ret = 0; // javaのResultSet.getInt()はNULLの場合 0 を返すのでこれでいいのだ
            }
            else
            {
                ret = Convert.ToInt32( obj );
            }
            return ret;
        }

        public static short toShort( Object obj )
        {
            short ret;
            if ( obj is DBNull )
            {
                ret = 0; // javaのResultSet.getInt()はNULLの場合 0 を返すのでこれでいいのだ
            }
            else
            {
                ret = Convert.ToInt16( obj );
            }
            return ret;
        }

        public static bool toBool( Object obj )
        {
            bool ret;
            if ( obj is DBNull )
            {
                ret = false;
            }
            else
            {
                if ( Convert.ToInt32( obj ) != 0 )
                {
                    ret = true;
                }
                else
                {
                    ret = false;
                }
            }
            return ret;
        }

        public static Nullable<DateTime> toDateTime( Object obj )
        {
            Nullable<DateTime> ret;
            if ( obj is DBNull )
            {
                ret = null;
            }
            else
            {
                ret = Convert.ToDateTime( obj );
            }
            return ret;
        }
    }
}
